package org.smog.service.auth.impl;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.smog.core.config.security.jwt.JwtTokenUtil;
import org.smog.core.config.security.model.JwtToken;
import org.smog.core.config.security.model.JwtUser;
import org.smog.service.auth.IAuthService;
import org.smog.service.auth.dto.AuthInput;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.Properties;

/**
 * @Description: Created by IntelliJ IDEA.
 * @project_name: smogTemp
 * @time: 2020-05-14 15:53
 * @email: 17685306043@163.com
 * @author: huangZhongYao
 */
@Slf4j
@Service
@AllArgsConstructor
public class AuthServiceImpl implements IAuthService {

    JwtTokenUtil jwtTokenUtil;

    @Override
    public JwtToken login(AuthInput input) {
        Properties properties = System.getProperties();
        Collection<? extends GrantedAuthority> authorities = AuthorityUtils.createAuthorityList(new String[]{"ROLE_ADMIN"});
        JwtUser jwtUser = new JwtUser("1", input.getUserName(), "121212", input.getUserName(), input.getPassword(), authorities);
        return jwtTokenUtil.generateToken(jwtUser);
    }

    @Override
    public Boolean logout() {
        SecurityContextHolder.clearContext();
        return Boolean.TRUE;
    }
}
